package com.atguigu.srb.core.hfb;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.common.utils.HttpUtils;
import com.atguigu.common.utils.MD5;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

@Slf4j
public class RequestHelper {
    public static void main(String[] args) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("d","4");
        paramMap.put("c","3");
        paramMap.put("b","2");
        paramMap.put("a","1");
    }

    /**
     * 请求数据获取签名
     * @param paramMap
     * @return
     */
    public static String getSign(Map<String,Object>paramMap){
        if(paramMap.containsKey("sign")){
            paramMap.remove("sign");
        }
        TreeMap<String, Object> sorted = new TreeMap<>(paramMap);
        StringBuilder str = new StringBuilder();
        for(Map.Entry<String,Object>param:sorted.entrySet()){
            str.append(param.getValue()).append("|");
        }
        str.append(HfbConst.SIGN_KEY);
        log.info("加密前："+str.toString());
        String md5str = MD5.encrypt(str.toString());
        log.info("加密后："+md5str);
        return md5str;
    }

    /**
     * Map转换
     * @param paramMap
     * @return
     */
    public static Map<String,Object> switchMap(Map<String,String[]> paramMap){
        Map<String,Object> resultMap = new HashMap<>();
        for(Map.Entry<String,String[]> param:paramMap.entrySet()){
            resultMap.put(param.getKey(),param.getValue()[0]);
        }
        return resultMap;
    }

    /**
     * 签名校验
     * @param paramMap
     * @return
     */
    public static boolean isSignEquals(Map<String,Object>paramMap){
        String sign = (String)paramMap.get("sign");
        String md5Str = getSign(paramMap);
        if(!sign.equals(md5Str)){
            return false;
        }
        return true;
    }

    /**
     * 获取时间戳
     * @return
     */
    public static long getTimestamp(){
        return new Date().getTime();
    }

    /**
     * 封装同步请求
     * @param paramMap
     * @param url
     * @return
     */
    public static JSONObject sendRequest(Map<String,Object>paramMap,String url){
        String result="";
        try {
            StringBuilder postdata = new StringBuilder();
            for(Map.Entry<String,Object>param:paramMap.entrySet()){
                postdata.append(param.getKey()).append("=")
                        .append(param.getValue()).append("&");
            }
            log.info(String.format("--> 发送请求到汇付宝：post data %1s",postdata));
            byte[] reqData = postdata.toString().getBytes(StandardCharsets.UTF_8);
            byte[] respdata = HttpUtils.doPost(url, reqData);
            result = new String(respdata);
            log.info(String.format("-->汇付包应答结果：result data %1s",result));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return JSONObject.parseObject(result);
    }
}
